package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.core.validate.QueryGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.salary.domain.bo.SalaryCorrectionRecordBo;
import org.eiahe.hr.salary.domain.vo.SalaryCorrectionRecordVo;
import org.eiahe.hr.salary.service.ISalaryCorrectionRecordService;
import org.ehe.common.excel.utils.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 薪资纠错记录控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/correction")
public class SalaryCorrectionRecordController extends BaseController {

    private final ISalaryCorrectionRecordService iSalaryCorrectionRecordService;

    /**
     * 查询薪资纠错记录列表
     */
    @SaCheckPermission("salary:correction:list")
    @GetMapping("/list")
    public TableDataInfo<SalaryCorrectionRecordVo> list(SalaryCorrectionRecordBo bo, PageQuery pageQuery) {
        return iSalaryCorrectionRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出薪资纠错记录列表
     */
    @SaCheckPermission("salary:correction:export")
    @Log(title = "薪资纠错记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalaryCorrectionRecordBo bo, HttpServletResponse response) {
        List<SalaryCorrectionRecordVo> list = iSalaryCorrectionRecordService.queryList(bo);
        // 转换字段为汉字名称
        for (SalaryCorrectionRecordVo vo : list) {
            // 转换纠错类型
            if ("1".equals(vo.getCorrectionType())) {
                vo.setCorrectionType("冲减");
            } else if ("2".equals(vo.getCorrectionType())) {
                vo.setCorrectionType("状态回退");
            } else if ("3".equals(vo.getCorrectionType())) {
                vo.setCorrectionType("数据修正");
            }

            // 转换审批状态
            if ("0".equals(vo.getApprovalStatus())) {
                vo.setApprovalStatus("待审批");
            } else if ("1".equals(vo.getApprovalStatus())) {
                vo.setApprovalStatus("审批通过");
            } else if ("2".equals(vo.getApprovalStatus())) {
                vo.setApprovalStatus("审批拒绝");
            }
        }
        ExcelUtil.exportExcel(list, "薪资纠错记录数据", SalaryCorrectionRecordVo.class, response);
    }

    /**
     * 获取薪资纠错记录详细信息
     *
     * @param correctionId 主键
     */
    @SaCheckPermission("salary:correction:query")
    @GetMapping("/{correctionId}")
    public R<SalaryCorrectionRecordVo> getInfo(@PathVariable("correctionId") Long correctionId) {
        return R.ok(iSalaryCorrectionRecordService.queryById(correctionId));
    }

    /**
     * 新增薪资纠错记录
     */
    @SaCheckPermission("salary:correction:add")
    @Log(title = "薪资纠错记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalaryCorrectionRecordBo bo) {
        return toAjax(iSalaryCorrectionRecordService.insertByBo(bo));
    }

    /**
     * 修改薪资纠错记录
     */
    @SaCheckPermission("salary:correction:edit")
    @Log(title = "薪资纠错记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalaryCorrectionRecordBo bo) {
        return toAjax(iSalaryCorrectionRecordService.updateByBo(bo));
    }

    /**
     * 删除薪资纠错记录
     *
     * @param correctionIds 主键串
     */
    @SaCheckPermission("salary:correction:remove")
    @Log(title = "薪资纠错记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{correctionIds}")
    public R<Void> remove(@PathVariable Long[] correctionIds) {
        return toAjax(iSalaryCorrectionRecordService.deleteWithValidByIds(Arrays.asList(correctionIds)));
    }

    /**
     * 审批薪资纠错记录
     */
    @SaCheckPermission("salary:correction:approve")
    @Log(title = "薪资纠错记录", businessType = BusinessType.UPDATE)
    @PostMapping("/approve/{correctionId}")
    public R<Void> approve(@PathVariable("correctionId") Long correctionId,
            @RequestParam("approvalStatus") String approvalStatus,
            @RequestParam("approver") String approver,
            @RequestParam(value = "approvalOpinion", required = false) String approvalOpinion) {
        return toAjax(iSalaryCorrectionRecordService.approveCorrection(correctionId, approvalStatus, approver,
                approvalOpinion));
    }

    /**
     * 执行薪资纠错操作
     */
    @SaCheckPermission("salary:correction:execute")
    @Log(title = "薪资纠错记录", businessType = BusinessType.UPDATE)
    @PostMapping("/execute/{correctionId}")
    public R<Void> execute(@PathVariable("correctionId") Long correctionId) {
        Boolean b = iSalaryCorrectionRecordService.executeCorrection(correctionId);
        System.out.println("执行薪资纠错操作b="+b);
        if(b){
            return R.ok("执行薪资纠错操作成功");
        }
        return R.fail("执行薪资纠错操作失败");
    }
}
